package com.lezheba.action;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.lezheba.api.JsonApi;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("rawtypes")
public class BaseAction extends ActionSupport {
	private static Logger log = Logger.getLogger(BaseAction.class);
	
	private static final long serialVersionUID = 1L;
	public static final String ENCODING_FROM = "ISO-8859-1"; // ISO-8859-1 GBK
														// GB18030 UTF-8
	public static final String ENCODING = "UTF-8";
	
	public static int TAG_SUCCESS = 1;
	public static int TAG_FAILED = -1;
	

	public String ajax(Object... objects) throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		for (Object object : objects) {
			out.print(object.toString());
		}

		return null;
	}

	public Map getSession() {
		return ActionContext.getContext().getSession();
	}

	public void putSession(Map<String, String> map) {
		for (Map.Entry<String, String> m : map.entrySet()) {
			putSession(m.getKey(), m.getValue());
		}

	}

	@SuppressWarnings("unchecked")
	public void putSession(String name, String value) {
		Map session = getSession();
		if (session != null) {
			session.put(name, value);
		}
	}

	public void removeSession(String name) {
		Map session = getSession();
		if (session != null) {
			session.remove(name);
		}
	}

	public String getSession(String name) {
		Map session = getSession();
		if (session != null) {
			return (String) session.get(name);
		}
		return null;
	}

	public String execute() {
		return SUCCESS;
	}

	public static String getAfterTranslate(String str) {
		log.info("str"+str);
		String strEn = null;
		if (str != null) {
			try {
				strEn = new String(str.getBytes(ENCODING_FROM), ENCODING);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		log.info("strEn"+strEn);
		return strEn;
	}

}
